# -*- py-indent-offset: 4 -*- 
# -*- tab-width: 8 -*- 
# -*- indent-tabs-mode: nil -*-

import CmdNo,string
from etgui import *

from PageLayout import *
from PrefsAppearance import *
from PrefsTools import *
from PrefsProjectSetup import *
from PrefsVCS import * 
from PrefsFileTypes import *
from PrefsPlatform import *
from PrefsOthers import *
from PrefsCrispParser import *

class LevelSwitcher:
	current_level=[None]
	def __init__(self,model,level):
		self.model=model
		self.level=level
	def __call__(self):
		if self.current_level[0]!=self.level:
			self.model.GotoLevel(self.level)
			self.current_level[0]=self.level

class PrefDialogInfo:
	def __init__(self,*levels):
		self.levels=levels
		self.dialog=None
		self.panel=None
		self.model=PotGuiPrefsModel()
	def ShowForTool(self,which_tool):
		if not self.dialog:
			self.dialog=self.MakeDialog()
		self.model.AllValuesChanged()
		if len(self.levels)==1:
			self.model.GotoLevel("USER")
		if self.pageindex.has_key(which_tool):
			self.panel.GotoPage(self.pageindex[which_tool])
		self.dialog.ShowUnderMouse()
	def MakeDialog(self):
		self.panel=PotGuiPanel(gPoint6,eVObjVExpand|eVObjHExpand)
		self.pageindex={}# start from the beginning
		model=EditPotGuiModel(self.model,1) #propagate Apply/Cancel to real model

		panel=self.panel

		# add the pagenumber to the self.pageindex
		self.pageindex["Appearance"]=len(self.pageindex)
		panel.AddPage(
			MakeHeader(
				"Appearance",
				"Change display appearance",
				MakePageAppearance(model)
			),
			0,
			"Appearance"
		)
		if IsWindows()!=1:   # missing colors page under windows
			self.pageindex["Colors"]=len(self.pageindex)
			panel.AddPage(
				MakeHeader(
					"Customizing Colors",
					"Change display color scheme",
					MakePageWindowColors(model)
				),
				1,
				"Motif Colors"
			)

		self.pageindex["Tools"]=len(self.pageindex)
		panel.AddPage(
			MakeHeader(
				"Tools",
				"Change settings of SNiFF+ tools",
				MakePageTools(model)
			),
			0,
			"Tools"
		)

		self.pageindex["Editor"]=len(self.pageindex)
		panel.AddPage(
			MakeHeader(
				"Source Editor",
				"Specify Source Editor settings",
				MakePageED(model)
			),
			1,
			"Source Editor",
			"sourceeditor"
		)

		self.pageindex["Retriever"]=len(self.pageindex)
		panel.AddPage(
			MakeHeader(
				"Retriever",
				"Specify Retriever settings",
				MakePageRE(model)
			),
			1,
			"Retriever","retriever"
		)

		self.pageindex["CrossReferencer"]=len(self.pageindex)
		panel.AddPage(
			MakeHeader(
				"Cross Referencer",
				"Specify Cross Referencer settings ",
				MakePageCR(model)
			),
			1,
			"Cross Referencer",
			"crossref"
		)

		self.pageindex["DocumentationEditor"]=len(self.pageindex)
		panel.AddPage(
			MakeHeader(
				"Documentation Editor",
				"Specify Documentation Editor settings",
				MakePageDE(model)
			),
			1,
			"Documentation Editor",
			"docubrowser"
		)

		self.pageindex["Shell"]=len(self.pageindex)
		panel.AddPage(
			MakeHeader(
				"Shell",
				"Specify Shell settings",
				MakePageSH(model)
			),
			1,
			"Shell",
			"shelltool"
		)

		self.pageindex["WorkingEnvTool"]=len(self.pageindex)
		panel.AddPage(
			MakeHeader(
				"Working Environments",
				"Specify Working Environments settings",
				MakePageWE(model)
			),
			1,
			"Working Environments",
			"workingenvtool"
		)
		if EnvGetBool("Crisp.VisibleInGUI",0):
			self.pageindex["ParserConfig"]=len(self.pageindex)
			panel.AddPage(
				MakeHeader(
					"Parser Configuration",
					"Specify settings for crsip parsing",
					MakePageCrispParsing(model)
				),
				0,
				"Parser Configuration"
			)


		self.pageindex["NewProject"]=len(self.pageindex)
		panel.AddPage(
			MakeHeader(
				"New Project Setup",
				"Specify default settings for new projects",
				MakePageProjectSetup(model)
			),
			0,
			"New Project Setup"
		)

		self.pageindex["VCS"]=len(self.pageindex)
		panel.AddPage(
			MakeHeader(
				"Version Control System",
				"Specify version control system",
				MakePageVCS(model)
			),
			0,
			"Version Control System"
		)

		self.pageindex["FileTypes"]=len(self.pageindex)
		panel.AddPage(
			MakeHeader(
				"File Types",
				"Specify different file types",
				MakePageFileTypes(model)
			),
			0,
			"File Types"
		)

		self.pageindex["Debugger"]=len(self.pageindex)
		panel.AddPage(
			MakeHeader(
				"Platform",
				"Specify platform specific settings",
				MakePagePlatform(model)
			),
			0,
			"Platform"
		)

		self.pageindex["Others"]=len(self.pageindex)
		panel.AddPage(
			MakeHeader(
				"Others",
				"Set general preferences for SNiFF+",
				MakePageOthers(model)
			),
			0,
			"Others"
	)
		panel.Update()
		if len(self.levels)>1:
			pages=[]
			for level in self.levels:
				page=PseudoTabPage(
					level+' '+"LEVEL",
					panel,
					action=LevelSwitcher(self.model,level)
				)
				pages.append(page)
			panel=TabSheet(
				single_line=1,
				on_top=1,
				items=pages
			)
		dialog=PotGuiDialog(
			model,
			MakeDialogInner(
				panel,
				ActionButton(CmdNo.cIdOk,"OK",1),
				ActionButton(CmdNo.cIdCancel,"Cancel"),
				ActionButton("Apply",action=model.Apply),
				ActionButton(CmdNo.cIdDefault,"Default"),
			),
			"Preferences",
			eWinDefault
		)
		return dialog


gDialogUSER=PrefDialogInfo("USER")

gDialogSITE=PrefDialogInfo("USER","SITE")

def Show(which_tool=None):
	#print "Calling Tool: %s" % which_tool
	if "SITE" in gDialogUSER.model.GetLevelNames(1):
		dialog=gDialogSITE
	else:
		dialog=gDialogUSER
	dialog.ShowForTool(which_tool)
